/*******************************************************************************
 * This file is part of Crunch Network.
 *
 * Crunch Network is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Crunch Network is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * Crunch Network.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package com.crunch.network;

/**
 * A {@code MessageDependency} represents a (guaranteed) message which has previously been sent on the network.
 *
 * When a guaranteed message is written, a {@code MessageDependency} instance can optionally be passed into the
 * {@code dependencyOut} parameter of the {@link NetworkWriter#writeMessage(Message, boolean, MessageDependency[],
 * MessageDependency)} method. The {@code MessageDependency} instance will be set to refer to the message that was
 * just written. When writing future messages, a list of {@code MessageDependency} instances is specified using the
 * {@code dependenciesIn} parameter. When a message is received, all messages in its dependency list must first be
 * executed before the message itself is executed.
 *
 * Initially, {@code MessageDependency} refer to no message. Passing an empty {@code MessageDependency} instance into
 * the {@code dependenciesIn} parameter has no effect. If a non-empty {@code MessageDependency} is passed into the
 * {@code dependencyOut} parameter, its previous reference will be overwritten. It is possible to pass the same {@code
 * MessageDependency} instance into both the {@code dependenciesIn} and {@code dependencyOut} parameters. In fact, this
 * is a convenient way to create a "channel" of ordered messages.
 *
 * Once a dependency for a message is no longer needed, it can safely be cleared even if dependant messages have not
 * yet been sent.
 *
 * Even if a {@code MessageDependency} instance is not explicitly cleared or overwritten, its
 * {@link MessageDependency#isEmpty()} method will eventually begin to return {@code true} after a period of time. This
 * occurs when it is verified that the message has been received on the other side of the connection and an explicit
 * dependency is no longer necessary to enforce message ordering. For more details on message dependencies, see
 * <TODO>blah</TODO>.
 */
public class MessageDependency {
	/**
	 * Constructs a {@code MessageDependency} instance referring to no message.
	 */
	public MessageDependency() {
		writer = null;
	}

	/**
	 * Clears the reference to the message, causing this {@code MessageDependency} instance to be empty.
	 */
	public void clear() {
		if (writer == null) {
			return;
		}
		writer.forgetMessageDependency(this);
		writer = null;
	}

	/**
	 * Returns whether the {@code MessageDependency} is empty.
	 *
	 * @return  whether the {@code MessageDependency} is empty.
	 */
	public boolean isEmpty() {
		return writer == null;
	}

	/**
	 * Returns the sequence number of the referenced message. The user should not deal directly with sequence numbers,
	 * so this method is not public.
	 *
	 * @return  the sequence number of the referenced message.
	 */
	int getSequenceNumber() {
		return sequenceNumber;
	}

	/**
	 * Returns the {@link NetworkWriter} instance used to set this dependency. Used by the {@link NetworkWriter}.
	 *
	 * @return the {@link NetworkWriter} instance used to set this dependency.
	 */
	NetworkWriter getWriter() {
		return writer;
	}

	/**
	 * Sets the reference to the message. This method should only be called from the {@link NetworkWriter} instance
	 * which is writing the message.
	 *
	 * @param   writer the {@link NetworkWriter} instance from which the message is being sent.
	 * @param   sequenceNumber the sequence number of the message.
	 */
	void set(NetworkWriter writer, int sequenceNumber) {
		clear();
		this.writer = writer;
		this.sequenceNumber = sequenceNumber;
	}

	// writer used to set this dependency
	private NetworkWriter writer;
	// the sequence number of the message referenced
	private int sequenceNumber;
}
